<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace crcl\base\mvc\request;

interface IConcrete
{
  /**
   * Returns call to mvc.
   *
   * defaults to "/"
   *
   * http: uri without extension
   * console: call param
   */
  public function getCall();

  /**
   * Returns method of request (get, post, etc)
   * On non http you have to simulate the method (esp on console)
   *
   * defaults to "GET"
   *
   * http REQUEST_METHOD
   * console: method param
   */
  public function getMethod();

  /**
   * Returns type of view
   *
   * http: extension in uri OR content-type header (todo)
   * console: view param
   *
   * defaults to "html"
   *
   * @return string;
   */
  public function getView();
}

abstract class Base implements IConcrete
{
  const DEFAULT_CALL   = '/';
  const DEFAULT_METHOD = 'GET';
  const DEFAULT_VIEW   = '';

  /** PROTECTED ***********************************************************************************/

  protected function cutExtension($sPath)
  {
    $oFile = \crcl\base\File::getInstance($sPath);
    if ($oFile->hasExtension())
    {
      $sExtension = $oFile->getExtension();
      $iPos = \strpos($sPath, $sExtension); //braucht nicht geprüft werden, da hasExtension = true
      $sPath = \substr($sPath, 0, $iPos);
      $sPath = \crcl\base\String::getInstance($sPath)->doNotEndWith('.')->toString();
    }
    return $sPath;
  }

  protected function cutParamsAndFragment($sInput)
  {
    if (\strlen($sInput))
    {
      if (\strstr($sInput, '?'))
      {
        $sInput = \substr($sInput, 0, \strpos($sInput, '?'));
      }
      //and the fragment
      if (\strstr($sInput, '#'))
      {
        $sInput = \substr($sInput, 0, \strpos($sInput, '#'));
      }
    }
    return $sInput;
  }
}